/** 
 * Description:首页推荐维护
 * Date:2013-1-31
 * Author:Along	
*/
package com.community.shopping.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.community.shopping.domain.IndexGroup;
import com.community.shopping.service.logic.IndexGroupManager;
import com.community.shopping.util.RequestHelp;
import com.community.shopping.service.logic.GoodsManager;
import com.community.shopping.domain.Goods;
@Controller 
public class IndexGroupControl {
	@Autowired  
    private IndexGroupManager indexGroupManager;
	@Autowired  
    private GoodsManager goodsManager;
	
	/**
	 * 推荐列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgrouplist")
	 public ModelAndView getlist(HttpServletRequest request) 
   { 
     ModelAndView modelAndView = new ModelAndView("index/group_list.jsp"); 
     Integer shopId=RequestHelp.getShopId(request);
     Integer kind=Integer.parseInt(request.getParameter("kind"));
     List groupGoodslist=indexGroupManager.getGroupGoods(shopId, kind);
	 modelAndView.addObject("groupGoodslist", groupGoodslist); 	 
	 modelAndView.addObject("kind", kind); 	 
     return modelAndView; 		
   } 
	
	/**
	 * 新增推荐
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgroupadd")
	 public ModelAndView addIndexGroup(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("index/group_add.jsp"); 
      Integer kind=Integer.parseInt(request.getParameter("kind"));
      modelAndView.addObject("kind", kind); 	 
      return modelAndView; 		
    } 
	/**
	 * 修改推荐
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgroupedit")
	 public ModelAndView editIndexGroup(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("index/group_edit.jsp"); 
	  String id=request.getParameter("id");
	  IndexGroup groupGoods=indexGroupManager.getIndexGroupById(Integer.parseInt(id));
	  modelAndView.addObject("groupGoods", groupGoods); 
     return modelAndView; 		
	 } 
	/**
	 * 保存推荐
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/indexgroupsave")
	 public ModelAndView save(HttpServletRequest request) throws Exception 
    { 
     // ModelAndView modelAndView = new ModelAndView("index/group_list.jsp");  
      Integer shopId=RequestHelp.getShopId(request);
      String ids=request.getParameter("ids");
      Integer kind=Integer.parseInt(request.getParameter("kind"));
      String[]goodsid=ids.split(",");
      int maxSort=indexGroupManager.getGroupGoodsMaxSort(shopId, kind);
      for(int i=0;i<goodsid.length;i++)
      {
    	  IndexGroup indexGroup=new IndexGroup();
    	  Goods goods=goodsManager.getGoodsById(Integer.parseInt(goodsid[i]));
    	  indexGroup.setShopId(RequestHelp.getShopId(request));
    	  indexGroup.setKind(kind);
    	  indexGroup.setGoods(goods);
    	  indexGroup.setSort(++maxSort);
    	  indexGroup.setStatus(1);
    	  indexGroupManager.save(indexGroup);
      }
      return getlist(request); 		
    } 
	/**
	 * 隐藏/显示推荐
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgroupstatus")
	 public ModelAndView hideindexgroup(HttpServletRequest request) 
    { 
     
	  String id=request.getParameter("id");
	  IndexGroup indexGroup=indexGroupManager.getIndexGroupById(Integer.parseInt(id));
	  indexGroup.setStatus(indexGroup.getStatus()==0?1:0);
	  ModelAndView modelAndView = new ModelAndView("indexgrouplist.do?kind="+indexGroup.getKind()); 
	  indexGroupManager.save(indexGroup);
     return modelAndView; 		
	 } 
	/**
	 * 删除推荐
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgroupdel")
	 public ModelAndView delindexgroup(HttpServletRequest request) 
    { 
     
	  String id=request.getParameter("id");
	  IndexGroup indexGroup=indexGroupManager.getIndexGroupById(Integer.parseInt(id));
	  ModelAndView modelAndView = new ModelAndView("indexgrouplist.do?kind="+indexGroup.getKind()); 
	  indexGroupManager.delete(indexGroup);
     return modelAndView; 		
	 } 
	/**
	 * 交换排序
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/indexgroupsort")
	 public ModelAndView sortindexgroup(HttpServletRequest request) 
    { 
     
	  String id=request.getParameter("id");
	  String targetid=request.getParameter("targetid");
	  IndexGroup indexGroup=indexGroupManager.getIndexGroupById(Integer.parseInt(id));
	  IndexGroup targetIndexGroup=indexGroupManager.getIndexGroupById(Integer.parseInt(targetid));
	  int currentSort=indexGroup.getSort();
	  indexGroup.setSort(targetIndexGroup.getSort());
	  targetIndexGroup.setSort(currentSort);
	  indexGroupManager.save(indexGroup);
	  indexGroupManager.save(targetIndexGroup);
	  ModelAndView modelAndView = new ModelAndView("indexgrouplist.do?kind="+indexGroup.getKind()); 	  
     return modelAndView; 		
	 } 

}
